package topic.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;

import com.cpoopc.scrollablelayoutlib.ScrollableHelper;
import com.cpoopc.scrollablelayoutlib.ScrollableLayout;
import com.example.xuegengwang.xuegengwang.R;
import com.google.gson.Gson;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import common.BaseActivity;
import common.BaseFragmentAdapter;
import common.Constant;
import https.InterNetController;
import topic.adapter.TopicGalleryAdapter;
import topic.bean.TopicGroupBean;
import topic.fragment.TopicFragment;
import view.MyPagerSlidingTabStrip;

/**
 * Created by Administrator on 2017/3/20
 * 热门讨论
 */
@ContentView(R.layout.topic_activity)
public class Topic extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.scrollableLayout)
    ScrollableLayout scrollableLayout;
    @ViewInject(R.id.topic_activity_gallery)
    private Gallery gallery;
    @ViewInject(R.id.common_back)
    private ImageView back;
    @ViewInject(R.id.common_title)
    private TextView title;
    @ViewInject(R.id.topic_activity_tabStrip)
    private MyPagerSlidingTabStrip myPagerSlidingTabStrip;
    @ViewInject(R.id.topic_activity_viewPager)
    private ViewPager topic_activity_viewPager;
    private String[] tabs = new String[]{"热门讨论热点", "我参与的讨论"};
    private android.support.v4.app.FragmentManager fragmentManager;
    private BaseFragmentAdapter baseFragmentAdapter;
    private ArrayList<Fragment> fragments;
    private TopicFragment fg1;
    private TopicFragment fg2;
    private int tabs_color, tabs_text_size, tabs_color_chosed;
    private TopicGalleryAdapter topicGalleryAdapter;//上面的话题列表
    private ArrayList<TopicGroupBean.TopicListBean> urls = new ArrayList<>();
    private TopicGroupBean topicGroupBean;


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS://成功返回
                    Gson gson = new Gson();
                    topicGroupBean = gson.fromJson((String) msg.obj, TopicGroupBean.class);
                    if (topicGroupBean != null) {
                        List<TopicGroupBean.TopicListBean> topicList = topicGroupBean.getTopicList();
                        if (topicList != null) {
                            urls.clear();
                            for (int i = 0; i < topicList.size(); i++) {
                                urls.add(topicList.get(i));
                            }
                            topicGalleryAdapter.notifyDataSetChanged();
                            gallery.setSelection(urls.size() / 2);
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        initView();
        initTabStrip();
        getDataFromInterNet(1);
    }

    private void initView() {
        back.setOnClickListener(this);
        title.setText("热门讨论");
        topicGalleryAdapter = new TopicGalleryAdapter(Topic.this, urls);
        gallery.setAdapter(topicGalleryAdapter);
        gallery.setSpacing(16);
        gallery.setSelection(urls.size() / 2);
        gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                topicGalleryAdapter.setSelectItem(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent i = new Intent(Topic.this, SingleTopic.class);
                i.putExtra("topicid", topicGroupBean.getTopicList().get(position).getId());
                i.putExtra("title", topicGroupBean.getTopicList().get(position).getTitle());
                i.putExtra("image", topicGroupBean.getTopicList().get(position).getImage());
                startActivity(i);
            }
        });

    }

    private void initData() {
        fragments = new ArrayList<>();
        fg1 = new TopicFragment("hot");
        fg2 = new TopicFragment("my");
        scrollableLayout.getHelper().setCurrentScrollableContainer(
                new ScrollableHelper.ScrollableContainer() {
                    @Override
                    public View getScrollableView() {
                        return fg1.getScrollableView();
                    }
                }

        );
        tabs_color_chosed = this.getResources().getColor(R.color.common_color);
        tabs_color = this.getResources().getColor(R.color.tab_color);
        tabs_text_size = this.getResources().getDimensionPixelSize(R.dimen.text_size);
    }


    private void initTabStrip() {
        fragmentManager = this.getSupportFragmentManager();
        fragments.add(fg1);
        fragments.add(fg2);
        baseFragmentAdapter = new BaseFragmentAdapter(fragmentManager, fragments, tabs);
        topic_activity_viewPager.setAdapter(baseFragmentAdapter);
        myPagerSlidingTabStrip.setTextColor(tabs_color);
        myPagerSlidingTabStrip.setTextSize(tabs_text_size);
        myPagerSlidingTabStrip.setDividerColor(Color.TRANSPARENT);
        myPagerSlidingTabStrip.setDividerPadding(1);
        myPagerSlidingTabStrip.setUnderlineHeight(0);
        myPagerSlidingTabStrip.setIndicatorColor(tabs_color_chosed);
        myPagerSlidingTabStrip.setIndicatorHeight(4);
        myPagerSlidingTabStrip.setTabPaddingLeftRight(32);
        myPagerSlidingTabStrip.setIndicatorPadding(30, 30);
        myPagerSlidingTabStrip.setSelectedTextColor(tabs_color_chosed);
        myPagerSlidingTabStrip.setViewPager(topic_activity_viewPager, null);
        myPagerSlidingTabStrip.setShouldExpand(true, null);
        myPagerSlidingTabStrip.setOnPageChangeListener(
                new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                    }

                    @Override
                    public void onPageSelected(final int position) {
                        scrollableLayout.getHelper().setCurrentScrollableContainer(
                                new ScrollableHelper.ScrollableContainer() {
                                    @Override
                                    public View getScrollableView() {
                                        return ((TopicFragment) fragments.get(position)).getScrollableView();
                                    }
                                }
                        );
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {
                    }
                }

        );
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_back:
                Topic.this.finish();
                break;
            default:
                break;
        }
    }

    private void getDataFromInterNet(int page) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("page", String.valueOf(page));
        hashMap.put("type", "hot");
        new InterNetController(Topic.this, Constant.SHOWBLOG, handler, hashMap, 0);
    }
}
